
package com.example.liang.basamovie.activity;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.widget.Toast;
import com.example.liang.basamovie.R;
import com.example.liang.basamovie.fragment.FindFragment;
import com.example.liang.basamovie.fragment.HomeFragment;
import com.example.liang.basamovie.fragment.ThemeFragment;
import com.example.liang.basamovie.fragment.UserFragment;


public class HomeActivity extends AppCompatActivity {
    TabLayout tabLayout;
    FragmentManager fragmentManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initView();
    }

    private void initView() {
        tabLayout = (TabLayout) findViewById(R.id.homePage_tabLayout);
        getCommitFragment(new HomeFragment());
        TabLayout.Tab tabHome = tabLayout.newTab();
        TabLayout.Tab tabTheme = tabLayout.newTab();
        TabLayout.Tab tabFind = tabLayout.newTab();
        TabLayout.Tab tabUser = tabLayout.newTab();
        tabLayout.addTab(tabHome.setIcon(R.drawable.selector_home).setTag("tag1"));
        tabLayout.addTab(tabTheme.setIcon(R.drawable.selector_theme).setTag("tag2"));
        tabLayout.addTab(tabFind.setIcon(R.drawable.selector_find).setTag("tag3"));
        tabLayout.addTab(tabUser.setIcon(R.drawable.selector_user).setTag("tag4"));
        //平分屏幕宽度
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if ("tag1".equals(tab.getTag())) {
                    getCommitFragment(new HomeFragment());
                } else if ("tag2".equals(tab.getTag())) {
                    getCommitFragment(new ThemeFragment());
                } else if ("tag3".equals(tab.getTag())) {
                    getCommitFragment(new FindFragment());
                } else if ("tag4".equals(tab.getTag())) {
                    getCommitFragment(new UserFragment());
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
    private void getCommitFragment(Fragment fragment){
        //加载默认的碎片
        fragmentManager = getSupportFragmentManager();
        //开启事物
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment_activity_home,fragment);
        //提交事物
        transaction.commit();
    }
    //设置连续两次点击退出（不超过２秒）
    long firstTime;
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        //KEYCODE_BACK 手机物理回退键　的常量
        if(keyCode==KeyEvent.KEYCODE_BACK){
            long secondTime = System.currentTimeMillis();
            if(secondTime-firstTime>2000){
                Toast.makeText(this,"再按一次退出",Toast.LENGTH_SHORT).show();
                firstTime=System.currentTimeMillis();//第一次点击时给firstTime　赋值
                return true;
            }else {
                finish();//关闭Activity
            }

        }
        return super.onKeyUp(keyCode, event);
    }
}
